package com.handpay.nocardpay.baselibrary.account.view.fragment;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.DelegateActivity;
import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.account.model.bean.Account;
import com.handpay.nocardpay.baselibrary.account.presenter.AccountPresenterImpl;
import com.handpay.nocardpay.baselibrary.account.view.ILoginView;
import com.handpay.nocardpay.baselibrary.account.view.IShowHideFragmentListener;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseErrorInfo;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.main.widget.GestureLockDisplayView;
import com.handpay.nocardpay.baselibrary.main.widget.GestureLockView;
import com.handpay.nocardpay.baselibrary.setting.view.fragment.SettingGesturePWDFragment;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.CommonUtils;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;
import com.handpay.nocardpay.baselibrary.utils.NoCardPayConstant;

import java.util.HashMap;

/**
 * Created by lyang on 2017/10/16.
 */

public class GestureLoginFragment extends BaseFragment implements View.OnClickListener, ILoginView {
    private GestureLockDisplayView gesture_lock_display_view;
    private GestureLockView gesture_lock_view;
    private TextView gesture_other_login_type_tv, gesture_login_status_tv, gesture_login_user_phone_no_tv;
    private IShowHideFragmentListener showHideFragmentListener;
    private HashMap<String, Object> paramers;
    private AccountPresenterImpl mLoginLoginPresenter;
    private int iLoginLeftCount = 3;

    public static GestureLoginFragment newInstance() {
        Bundle args = new Bundle();
        GestureLoginFragment fragment = new GestureLoginFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_gesture_login;
    }

    @Override
    public void initView(View view) {
        gesture_other_login_type_tv = (TextView) view.findViewById(R.id.gesture_other_login_type_tv);
        gesture_login_status_tv = (TextView) view.findViewById(R.id.gesture_login_status_tv);
        gesture_login_user_phone_no_tv = (TextView) view.findViewById(R.id.gesture_login_user_phone_no_tv);
        gesture_lock_view = (GestureLockView) view.findViewById(R.id.gesture_lock_view);
        gesture_lock_view.setKey(ClientEngine.getInstance().getRMSData(BaseProtocolKey.KEY_GESTURE_PWD));
        gesture_lock_view.setOnGestureFinishListener(new GestureLockView.OnGestureFinishListener() {
            @Override
            public void OnGestureFinish(boolean success, String key) {
                HPLog.i(HPLog.YL_TAG, "success:" + success + "key:" + key);
                if (success) {
                    GestureLoginFragment.this.setFragmentResult(SettingGesturePWDFragment.RESULT_VERIFY_GESTURE_PWD_SUCCESS, null);
                    GestureLoginFragment.this.pop();
                    String phone = ClientEngine.getInstance().getRMSData(BaseProtocolKey.NOCARDPAY_NAME);
                    paramers.put(BaseProtocolKey.MOBILE, SecureManager.getInstance().des(phone, 1, null));
                    paramers.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
                    paramers.put(BaseProtocolKey.GESTURE_PWD, SecureManager.getInstance().des(key, 1, null));
                    mLoginLoginPresenter.gestureLogin(paramers, Account.class, true);
                } else {
                    if (key.length() < 4) {
                        showStatus(getString(R.string.gesture_other_login_limit_tips));
                        return;
                    }
                    if (iLoginLeftCount == 0) {
                        ClientEngine.getInstance().removedData(BaseProtocolKey.KEY_GESTURE_PWD);
                        DialogUtils.showAlertDialog(_mActivity, _mActivity.getString(R.string.dialog_title), _mActivity.getString(R.string.gesture_other_login_fail_tips), false, _mActivity.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                showHideFragmentListener.showFragment(NoCardPayConstant.NOCARDPAY_LOGIN);
                            }
                        });
                        return;
                    }
                    showStatus(String.format(getString(R.string.gesture_other_login_left_times), iLoginLeftCount));
                    iLoginLeftCount--;
                }
            }
        });
    }

    private void  showStatus(String status){
        gesture_login_status_tv.setVisibility(View.VISIBLE);
        gesture_login_status_tv.setText(status);
        TranslateAnimation animation = new TranslateAnimation(-20, 20, 0, 0);
        animation.setDuration(50);
        animation.setRepeatCount(2);
        animation.setRepeatMode(Animation.REVERSE);
        gesture_login_status_tv.startAnimation(animation);
        Handler handler =   new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                gesture_login_status_tv.setVisibility(View.INVISIBLE);
            }
        };
        handler.sendEmptyMessageDelayed(1,1000);
    }


    @Override
    public void initListener() {
        gesture_other_login_type_tv.setOnClickListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        paramers = new HashMap<>();
        mLoginLoginPresenter = new AccountPresenterImpl(_mActivity);
        mLoginLoginPresenter.setmLoginView(this);

    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        String phone = ClientEngine.getInstance().getRMSData(BaseProtocolKey.NOCARDPAY_NAME);
        gesture_login_user_phone_no_tv.setText(CommonUtils.formatPhoneNumber(phone));
    }

    @Override
    public String getFragmentTitle() {
        return _mActivity.getString(R.string.login_gesture_pwd);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.gesture_other_login_type_tv) {
            showHideFragmentListener.showFragment(NoCardPayConstant.NOCARDPAY_LOGIN);
        }
    }

    public void setShowHideFragmentListener(IShowHideFragmentListener showHideFragmentListener) {
        this.showHideFragmentListener = showHideFragmentListener;
    }

    @Override
    public void navigateToHome() {
        _mActivity.startActivity(new Intent(_mActivity, DelegateActivity.class));
        _mActivity.finish();
    }

    @Override
    public void loginError(String errorMsg) {
        ClientEngine.getInstance().removedData(BaseProtocolKey.KEY_GESTURE_PWD);
        DialogUtils.showAlertDialog(_mActivity, _mActivity.getString(R.string.dialog_title), errorMsg, false, _mActivity.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showHideFragmentListener.showFragment(NoCardPayConstant.NOCARDPAY_LOGIN);
            }
        });
    }

    @Override
    public void noSessionId() {

    }

    @Override
    public void showErrorAlert(BaseErrorInfo baseErrorInfo) {
        ClientEngine.getInstance().removedData(BaseProtocolKey.KEY_GESTURE_PWD);
        DialogUtils.showAlertDialog(_mActivity, _mActivity.getString(R.string.dialog_title), baseErrorInfo.errMessage, false, _mActivity.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showHideFragmentListener.showFragment(NoCardPayConstant.NOCARDPAY_LOGIN);
            }
        });
    }
}